/**
 *   Copyright (C) 2012  Development Solutions (DS) Software
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provided with
 *   this file.
 *      1. Preserve the original authors’ name.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 * 
 */
package org.dss.gil.service;

import java.awt.Component;
import java.io.File;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.dss.gil.util.Globals;

/** 
 * 
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.0
 */
public class MessagesService {

    private FileNameExtensionFilter extFilter;
    
    private String defInfoTitle;
    private String defQuestTitle;
    private String defWarnTitle;
    private String defErrorTitle;
    private String defInputTitle;
    private File lastPath = new File(Globals.PROGRAMS_FOLDER);
   
    private ImageIcon questImg;
    private ImageIcon infoImg;
    private ImageIcon errorImg;
    private ImageIcon warnImg;
    private JFileChooser openChooser;
    private JFileChooser saveChooser;
    
    //Messages Types
    private final int ERROR_MESSAGE = 0;
    private final int INFORMATION_MESSAGE = 1;
    private final int WARNING_MESSAGE = 2;
    private final int QUESTION_MESSAGE = 3;
    
    private final int DEFAULT_OPTION = 0;
    public final int YES_OPTION = 0;
    public final int NO_OPTION = 1;
    public final int CANCEL_OPTION = 2;
    
    public final int YES_NO_OPTION = 0;
    public final int YES_NO_CANCEL_OPTION = 1;
    
    private String confirmOpt[] = new String[]{Globals.language.getMessage("button.confirm")};
    private String aceptCancelOpt[] = new String[]{Globals.language.getMessage("button.confirm"),
                                                   Globals.language.getMessage("button.cancel") };
    private String yesNoOpt[] = new String[]{Globals.language.getMessage("button.yes"),
                                                        Globals.language.getMessage("button.no")};
    private String yesNoCancelOpt[] = new String[]{
                                                Globals.language.getMessage("button.yes"),
                                                Globals.language.getMessage("button.no"),
                                                Globals.language.getMessage("button.cancel")};
  

    /*********************************************************************
     ********************* S I N G L E T O N *****************************
     *********************************************************************/
    
    private static MessagesService INSTANCE = null;

    public static MessagesService getInstance() {
        if (INSTANCE == null)
            INSTANCE = new MessagesService();
        return INSTANCE;
    }

    private MessagesService(){
        errorImg = new ImageIcon(getClass().getResource(Globals.ERROR_IMG_PATH));
        infoImg = new ImageIcon(getClass().getResource(Globals.INFO_IMG_PATH));
        questImg = new ImageIcon(getClass().getResource(Globals.QUESTION_IMG_PATH));
        warnImg = new ImageIcon(getClass().getResource(Globals.WARNING_IMG_PATH));
        defWarnTitle = Globals.language.getMessage("title.warning");
        defQuestTitle = Globals.language.getMessage("title.question");
        defInfoTitle = Globals.language.getMessage("title.info");
        defErrorTitle = Globals.language.getMessage("title.error");
        defInputTitle = Globals.language.getMessage("title.input");
        extFilter = new FileNameExtensionFilter(Globals.FILTER_NAME,Globals.EXTENSIONS);
    }

    /*********************************************************************
     ********************* M E S S A G E S *******************************
     *********************************************************************/

    public void showInformationMessage(Component c, String message, String title){
        title = (title == null)?defInfoTitle:title;
        JOptionPane.showOptionDialog(c, message, title,YES_NO_OPTION,INFORMATION_MESSAGE,infoImg,confirmOpt,confirmOpt[DEFAULT_OPTION]);
    }

    public void showErrorMessage(Component c,String message, String title){
        title = (title == null)?defErrorTitle:title;
        JOptionPane.showOptionDialog(c, message, title,YES_NO_OPTION,ERROR_MESSAGE,errorImg,confirmOpt,confirmOpt[DEFAULT_OPTION]);
    }

    public void showWarningMessage(Component c,String message, String title){
        title = (title == null)?defWarnTitle:title;
        JOptionPane.showOptionDialog(c, message, title,YES_NO_OPTION,WARNING_MESSAGE,warnImg,confirmOpt,confirmOpt[DEFAULT_OPTION]);
    }

    public int showQuestionMessage(Component c,String question, String title){
        title = (title == null)?defQuestTitle:title;
        int opc= JOptionPane.showOptionDialog(c, question,title,YES_NO_OPTION,QUESTION_MESSAGE,questImg,yesNoOpt,yesNoOpt[NO_OPTION]);
        return opc;
    }

    public int showQuestionMessage(Component c,String question, String title,String [] options){
        title = (title == null)?defQuestTitle:title;
        int opc= JOptionPane.showOptionDialog(c, question,title,YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,questImg,options,options[DEFAULT_OPTION]);
        return opc;
    }
        
    public int showQuestionMessageCancel(Component c,String question, String title){
        title = (title == null)?defQuestTitle:title;
        int opc= JOptionPane.showOptionDialog(c, question,title,YES_NO_CANCEL_OPTION,QUESTION_MESSAGE,questImg,yesNoCancelOpt,yesNoCancelOpt[YES_OPTION]);
        return opc;
    }

    public String showInputMessage(Component c,String question, String title, String defaultText){
        title = (title == null)?defInputTitle:title;
        String input = (String) JOptionPane.showInputDialog(c, question, title, QUESTION_MESSAGE, questImg, null, defaultText);
        return input;
    }

    public String showInputMessage(Component c,String question, String title,String [] options, String defaultText){
        title = (title == null)?defInputTitle:title;
        String input = (String) JOptionPane.showInputDialog(c, question, title, QUESTION_MESSAGE, questImg, options, defaultText);
        return input;
    }
    
    public File[] openFileChooser(Component parent){
        openChooser = new JFileChooser();
        openChooser.setMultiSelectionEnabled(true);
        openChooser.setCurrentDirectory(lastPath);
        openChooser.setFileFilter(extFilter);
        openChooser.setAcceptAllFileFilterUsed(true);
        openChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        File[] files=null;
        if(openChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION){
            files = openChooser.getSelectedFiles();
            lastPath = (files.length==0)?lastPath:new File(files[0].getAbsolutePath());
        }
        openChooser = null;
        return files;
    }

    public File saveFileChooser(String title,Component comp){
        saveChooser = new JFileChooser();
        saveChooser.setCurrentDirectory(lastPath);
        int res = saveChooser.showSaveDialog(comp);
        File file=null;
        if(res == JFileChooser.APPROVE_OPTION){
            lastPath = new File(saveChooser.getSelectedFile().getPath());
            file = saveChooser.getSelectedFile();
        }
        saveChooser = null;
        return file;
    }

}
